const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    info: null,
    coupon_list:[],
    goods_list:[],
    navigator: { hide: 0 },
    anli_list:[],
    dianping_list: [],
    tabs: [{ title: '优惠', icon: 'gif' }, { title: '爆款', icon: 'hot' }, { title: '案例', icon: 'log' }, { title: '点评', icon:'review-null'}],
    tabIndex: 0,
    src:[],
    is_myshop: false,
    fenxiangzhuan: {
      show: false,
      width: '86rpx;',
      height: '88rpx;',
      bottom: '380rpx;',
      top: '10rpx'
    }
  },
  submitInfo: function (e) {
    var t = this;
    var shop_id = e.detail.target.dataset.shop_id;
    var formid = e.detail.formId;
    app.ihttpPost("https://www.58zltc.com/api.php/Index/addFormId", { form_id: formid, openid: app.globalData.openid }, function (res) { })
    wx.navigateTo({
      url: '/pages/shop/yuyue/yuyue?shop_id=' + shop_id,
    })

  },
  //小程序码
  qrcode: function (e) {
    var that = this
    wx.showLoading({
      title: '拼命生成中',
    })
    console.log(that.data.info.shop_id + '--' + that.data.token)
    wx.request({
      url: 'https://www.58zltc.com/api.php/Qrcode/getShopWxaCode/shop_id/' + that.data.info.shop_id + '/token/' + that.data.token,
      header: {
        'content-type': 'application/json' // 默认值
      },
      success: function (res) {
        var url = res.data.data.url;
        var urls = [];
        urls.push(url);
        wx.previewImage({
          urls: urls
        })
      },
      complete: function (e) {
        wx.hideLoading();
      }
    })
  },
  previewImage: function (e) {
    let that = this;
    var img_url = e.currentTarget.dataset.url;
    wx.previewImage({
      current: img_url,
      urls: [img_url]
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let extConfig = wx.getExtConfigSync ? wx.getExtConfigSync() : {};
    this.setData({
      token: extConfig.appid,
      shop_id:options.shop_id
    })
    var scene = decodeURIComponent(options.scene);
    if (scene != 'undefined') {
      var shop_id = scene;
    } else {
      var shop_id = options.shop_id;
    }
    var that = this;
    //快速导航
    app.ihttpGet("https://www.58zltc.com/api.php/Index/getNav", function (res) {
      var v = that.data.navigator;
      v.info = res.data
      that.setData({
        navigator: v
      })
    })
    //屏幕宽度
    wx.getSystemInfo({
      success: function (res) {
        var width = res.screenWidth;
        var v = that.data.navigator;
        v.width = width
        that.setData({
          navigator: v
        })
      },
    })

    //实例化一个动画
    this.animation = wx.createAnimation({
      // 动画持续时间，单位ms，默认值 400
      duration: 300,
      /**
       * linear 动画一直较为均匀
       * ease  从匀速到加速在到匀速
       * ease-in 缓慢到匀速
       * ease-in-out 从缓慢到匀速再到缓慢
       * step-start 动画一开始就跳到 100% 直到动画持续时间结束 一闪而过
       * step-end  保持 0% 的样式直到动画持续时间结束    一闪而过
       */
      timingFunction: 'ease',
      // 延迟多长时间开始
      delay: 100,
      /**
       * 以什么为基点做动画 效果自己演示
       * left,center right是水平方向取值，对应的百分值为left=0%;center=50%;right=100%
       * top center bottom是垂直方向的取值，其中top=0%;center=50%;bottom=100%
       */
      transformOrigin: 'right',
      success: function (res) {

      }
    })
    var width = this.data.navigator.width;
    var offset = width * 0.64;
    that.animation.translateX(offset).step()
    var v = this.data.navigator
    v.animation = that.animation.export()
    v.hide = 1
    that.setData({
      //输出动画
      navigator: v
    })

    app.ihttpGet('https://www.58zltc.com/api.php/Shop/item/shop_id/' + shop_id, res=>{
      if (res.data.openid == app.globalData.userInfo.openid) {
        that.setData({
          is_myshop: true
        })
      }
      this.setData({
        info: res.data
      })
    })
    app.ihttpGet('https://www.58zltc.com/api.php/Goods/index/shop_id/' + shop_id, res=>{
      this.setData({
        goods_list: res.data
      })
    })
    app.ihttpGet('https://www.58zltc.com/api.php/Goods/index/type/jiancai/shop_id/' + shop_id, res => {
      this.setData({
        jiancai_list: res.data
      })
    })
    app.ihttpGet('https://www.58zltc.com/api.php/Coupon/index/shop_id/' + shop_id, res=>{
      this.setData({
        coupon_list: res.data
      })
    })
    app.ihttpGet('https://www.58zltc.com/api.php/Jiancai/anli/shop_id/' + shop_id, res => {
      this.setData({
        anli_list: res.data
      })
    })
  },
  onReady: function () {
    app.confirmCommission()
  },
  //快捷导航
  navigator: function (e) {
    var width = this.data.navigator.width
    if (this.data.navigator.hide == 1) {
      var offset = '0'
      var mum = 0;
    } else {
      var offset = width * 0.64
      var mum = 1;
    }
    this.animation.translateX(offset).step()
    var v = this.data.navigator
    v.animation = this.animation.export();
    v.hide = mum;
    this.setData({
      //输出动画
      navigator: v
    })
  },

  setTab: function (e) {
    this.setData({
      tabIndex: e.currentTarget.dataset.index
    })
  },
  bindCall: function (e) {
    console.log(e);
    var tel = e.currentTarget.dataset.tel;
    console.log(tel);
    wx.makePhoneCall({
      phoneNumber: tel
    })
  },
  openLocation: function (e) {
    var data = e.currentTarget.dataset;
    wx.openLocation({
      latitude: Number(data.latitude),
      longitude: Number(data.longitude),
      name: data.name,
      address: data.address
    })
  },
  getCoupon: function (e) {
    let that = this;
    const id = e.currentTarget.dataset.id;
    var post_data = {};
    post_data.coupon_id = id;
    post_data.openid = app.globalData.openid;
    app.ihttpPost("https://www.58zltc.com/api.php/Coupon/getCoupon", post_data, function (res) {
      app.iModel(res.data.err_msg);
    })
  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    let that = this;
    const shop = this.data.info;
    return {
      title: shop.name,
      path: '/pages/shop/jiancai/detail?shop_id=' + shop.shop_id + '&fenxiang=' + app.globalData.openid + '&fWid=' + app.globalData.wid,
      success: function (res) {
        var a = that.data.fenxiangzhuan
        a.show = false
        that.setData({
          fenxiangzhuan: a
        })
        // 转发成功
        wx.showToast({
          title: '转发成功！',
        })
      },
      fail: function (res) {
        // 转发失败
      }
    }
  },
  copyTBL: function (e) {
    var that = this;
    var pages = getCurrentPages();
    var currentPage = pages[pages.length - 1];
    var url = currentPage.route;
    var options = currentPage.options;
    wx.setClipboardData({
      data: '/' + url + '?shop_id=' + options.shop_id,
      success: function (res) {
        // self.setData({copyTip:true}),  
        wx.showModal({
          title: '提示',
          content: '复制成功',
          success: function (res) {
            if (res.confirm) {
              console.log('确定')
            } else if (res.cancel) {
              console.log('取消')
            }
          }
        })
      }
    });
  },
  changeFXZ: function () {
    var that = this;
    var a = that.data.fenxiangzhuan
    a.show ? a.show = false : a.show = true
    that.setData({
      fenxiangzhuan: a
    })
  },
  jumpWeb: function () {
    var that = this
    if (that.data.info.web_link) {
      wx.navigateTo({
        url: '/pages/index/web/web?url=' + that.data.info.web_link,
      })
    }
  },
  have_VIP: function (e) {
    console.log(e.currentTarget.dataset.shop_id)
    let that = this
    var shop_id = e.currentTarget.dataset.shop_id
    var my_openid = app.globalData.openid
    app.ihttpGet("https://www.58zltc.com/api.php/MyCard/index/openid/" + my_openid + '/shop_id/' + shop_id, function (res) {
      var data = res.data
      //有会员卡直接跳转
      if (data.mine[0]) {
        wx.navigateTo({
          url: '/pages/mine/shop/mycard/mycarddetail?id=' + data.mine[0].id,
        })
      }
      //没有会员卡提示开通
      else {
        wx.showModal({
          title: '提示',
          content: '请先开通会员',
          success: function (resq) {
            if (resq.confirm) {
              console.log('用户点击确定')
              //店铺是否添加会员卡
              app.ihttpGet("https://www.58zltc.com/api.php/MyCard/haveShopVipCard/shop_id/" + shop_id, function (res_1) {
                if (res_1.data != 0) {
                  wx.navigateTo({
                    url: '/pages/mine/shop/mycard/detail?id=' + res_1.data.id,
                  })
                }
                else {
                  wx.hideLoading()
                  setTimeout(function () {
                    wx.showToast({
                      title: '店铺未添加会员卡',
                      icon: 'none',
                      duration: 2000
                    })
                  }, 100)
                }
              })
            } else if (resq.cancel) {
              console.log('用户点击取消')
            }
          }
        })
      }
    })
  }
})